import axios from 'axios'
import store from '@/store'
import { Notice } from 'iview'
import router from '@/router'
import {
  getToken
} from '@/libs/util'
// import { Spin } from 'iview'
const addErrorLog = errorInfo => {
  const { statusText, status, request: { responseURL } } = errorInfo
  let info = {
    type: 'ajax',
    code: status,
    mes: statusText,
    url: responseURL
  }
  if (!responseURL.includes('save_error_logger')) store.dispatch('addErrorLog', info)
}

class HttpRequest {
  constructor (baseUrl = baseURL) {
    this.baseUrl = baseUrl
    this.queue = {}
  }
  getInsideConfig () {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        //
      }
    }
    return config
  }
  destroy (url) {
    delete this.queue[url]
    if (!Object.keys(this.queue).length) {
      // Spin.hide()
    }
  }
  interceptors (instance, url) {
    // 请求拦截
    instance.interceptors.request.use(config => {
      // 设置请求头信息
      if (url === '/api/auth/oauth/token') {
        config.headers.Authorization = 'Basic QWNjb3VudDpBY2NvdW50' // 增加客户端认证
      } else {
        config.headers.Authorization = getToken() // 让每个请求携带token
      }
      // if(store.state.user.token) {
      //   config.headers.Authorization = store.state.user.token // 让每个请求携带token
      // } else {
      //   config.headers.Authorization = 'Basic Y2xpZW50OmNsaWVudA==' // 增加客户端认证
      // }

      // 添加全局的loading...
      if (!Object.keys(this.queue).length) {
        // Spin.show() // 不建议开启，因为界面不友好
      }
      this.queue[url] = true
      return config
    }, error => {
      return Promise.reject(error)
    })
    // 响应拦截
    instance.interceptors.response.use(res => {
      this.destroy(url)
      const response = res.data
      if (response.status !== 200 && res.status !== 200) {
        Notice.error({
          title: '错误信息',
          desc: '系统出现未知错误'
        })
      } else {
        return res.data
      }
    }, error => {
      console.log('获取的token' + store.state.user.token)
      this.destroy(url)
      let response = error.response
      if (response === undefined) {
        Notice.error({
          title: '错误信息',
          desc: '服务请求超时',
          duration: 5
        })
        return Promise.reject(error)
      }
      if (response.status === 500) {
        console.log(response)
        Notice.error({
          title: '错误信息',
          desc: response.data.message,
          duration: 5
        })
        return Promise.reject(error)
      }
      if (response.status === 400) {
        console.log(response)
        Notice.error({
          title: '请求无效',
          desc: response.data.message || response.data.error_description,
          duration: 5
        })
        return Promise.reject(error)
      }
      if (response.status === 401) {
        Notice.error({
          title: '错误信息',
          desc: '登录信息已失效，请重新登录',
          duration: 5
        })
        store.dispatch('handleLogOut').then(() => {
          router.push({
            name: 'login'
          })
        })
        return Promise.reject(error)
      }
      if (response.status === 403) {
        Notice.error({
          title: '错误信息',
          desc: '没有权限操作',
          duration: 5
        })
        if (response.data.status === 40201) {
          store.dispatch('handleLogOut').then(() => {
            router.push({
              name: 'login'
            })
          })
        }
        return Promise.reject(error)
      }
      return Promise.reject(error)
    })
  }
  request (options) {
    const instance = axios.create()
    instance.defaults.headers.common['Authorization'] = '1111111111'
    options = Object.assign(this.getInsideConfig(), options)
    this.interceptors(instance, options.url)
    return instance(options)
  }
}
export default HttpRequest
